home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / Apps / DevTools / eText5 / Source / 8Bawl.bproj / 8Bawl(dis).class < prev    next >
Encoding:
Text File  |  1994-05-22  |  7.8 KB  |  231 lines

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //    FILENAME:    8Bawl.m
  3. //    SUMMARY:    an Eight-Ball (TM) Annotation
  4. //    SUPERCLASS:    Object
  5. //    INTERFACE:    None
  6. //    PROTOCOLS:    <Annotation, Tool>
  7. //    AUTHOR:        Eric P. Scott, ported to eText by Rohit Khare
  8. //    COPYRIGHT:    (c) 1994 California Institure of Technology, eText Project
  9. //    COPYRIGHT:    (c) 1994 San Francisco State University
  10. ///////////////////////////////////////////////////////////////////////////////
  11. //    DESCRIPTION
  12. //        An example of how to port over a simple View-based widget. Original
  13. //    application and 8Bawl source code by Eric P. Scott, Tech alum @ SFSU
  14. ///////////////////////////////////////////////////////////////////////////////
  15. //             BawlApp.m: a toy for the virtual desktop
  16. //             Eric P. Scott, San Francisco State University, January 1994
  17. ///////////////////////////////////////////////////////////////////////////////
  18. //    HISTORY
  19. //    05/22/94:    Created. First eText port.
  20. ///////////////////////////////////////////////////////////////////////////////
  21.  
  22. #import "../eTextKernel.h"
  23. #include "wraps8.h"
  24. #include <bsd/libc.h>
  25.  
  26. #define    NIMAGES 21
  27. @class EBI;
  28.  
  29. // File-Scope Global shared by all 8Bawls
  30. id    face[NIMAGES];
  31. id    base8;
  32. id    big8;
  33. BOOL initted=NO;
  34.  
  35.  
  36. @interface EightBawl:Object <Annotation, Tool>
  37. {    BOOL    highlighted;
  38. }
  39. @end
  40.  
  41. @implementation EightBawl
  42.  
  43. const NXRect bigRect={ { 0.0, 0.0 }, { 288.0, 288.0 } };
  44. const NXPoint smallRectOrigin={ 96.0, 88.0}, smallRectUpOrigin={ 96.0, 116.0};
  45. const NXSize smallRectSize={ 96.0, 84.0};
  46.  
  47. + toolAwake:theApp
  48. {
  49.     long         now;
  50.     int         i;
  51.     char        buf[MAXPATHLEN];
  52.     NXBundle    *bundle;
  53.     
  54.     (void)time(&now);
  55.     srandom((int)now^(0xffffff));
  56.     if (!initted) {
  57.     for(i=0; i < NIMAGES; i++) face[i] = nil;
  58.     bundle = [NXBundle bundleForClass:[EightBawl class]];
  59.     if ([bundle getPath:buf forResource:"Base8" ofType:"eps"] )
  60.         base8 = [[NXImage alloc] initFromFile:buf];
  61.     if ([bundle getPath:buf forResource:"Big8" ofType:"eps"] )
  62.         big8 = [[NXImage alloc] initFromFile:buf];
  63.     if ([bundle getPath:buf forResource:"small8" ofType:"tiff"] )
  64.         face[0] = [[NXImage alloc] initFromFile:buf];
  65.     if ([bundle getPath:buf forResource:"ask_again" ofType:"tiff"] )
  66.         face[1] = [[NXImage alloc] initFromFile:buf];
  67.     if ([bundle getPath:buf forResource:"as_i_see_it" ofType:"tiff"] )
  68.         face[2] = [[NXImage alloc] initFromFile:buf];
  69.     if ([bundle getPath:buf forResource:"better_not" ofType:"tiff"] )
  70.         face[3] = [[NXImage alloc] initFromFile:buf];
  71.     if ([bundle getPath:buf forResource:"certain" ofType:"tiff"] )
  72.         face[4] = [[NXImage alloc] initFromFile:buf];
  73.     if ([bundle getPath:buf forResource:"cannot_predict" ofType:"tiff"] )
  74.         face[5] = [[NXImage alloc] initFromFile:buf];
  75.     if ([bundle getPath:buf forResource:"concentrate" ofType:"tiff"] )
  76.         face[6] = [[NXImage alloc] initFromFile:buf];
  77.     if ([bundle getPath:buf forResource:"decidedly" ofType:"tiff"] )
  78.         face[7] = [[NXImage alloc] initFromFile:buf];
  79.     if ([bundle getPath:buf forResource:"may_rely" ofType:"tiff"] )
  80.         face[8] = [[NXImage alloc] initFromFile:buf];
  81.     if ([bundle getPath:buf forResource:"outlook_good" ofType:"tiff"] )
  82.         face[9] = [[NXImage alloc] initFromFile:buf];
  83.     if ([bundle getPath:buf forResource:"definitely" ofType:"tiff"] )
  84.         face[10] = [[NXImage alloc] initFromFile:buf];
  85.     if ([bundle getPath:buf forResource:"outlook_not" ofType:"tiff"] )
  86.         face[11] = [[NXImage alloc] initFromFile:buf];
  87.     if ([bundle getPath:buf forResource:"reply_hazy" ofType:"tiff"] )
  88.         face[12] = [[NXImage alloc] initFromFile:buf];
  89.     if ([bundle getPath:buf forResource:"dont_count" ofType:"tiff"] )
  90.         face[13] = [[NXImage alloc] initFromFile:buf];
  91.     if ([bundle getPath:buf forResource:"reply_no" ofType:"tiff"] )
  92.         face[14] = [[NXImage alloc] initFromFile:buf];
  93.     if ([bundle getPath:buf forResource:"signs_point" ofType:"tiff"] )
  94.         face[15] = [[NXImage alloc] initFromFile:buf];
  95.     if ([bundle getPath:buf forResource:"doubtful" ofType:"tiff"] )
  96.         face[16] = [[NXImage alloc] initFromFile:buf];
  97.     if ([bundle getPath:buf forResource:"sources_say" ofType:"tiff"] )
  98.         face[17] = [[NXImage alloc] initFromFile:buf];
  99.     if ([bundle getPath:buf forResource:"without_doubt" ofType:"tiff"] )
  100.         face[18] = [[NXImage alloc] initFromFile:buf];
  101.     if ([bundle getPath:buf forResource:"most_likely" ofType:"tiff"] )
  102.         face[19] = [[NXImage alloc] initFromFile:buf];
  103.     if ([bundle getPath:buf forResource:"yes" ofType:"tiff"] )
  104.         face[20] = [[NXImage alloc] initFromFile:buf];
  105.     initted = YES;
  106.     }
  107.     [theApp   registerAnnotation: [EightBawl class] 
  108.                             name: "EightBawl"
  109.                     RTFDirective: "EightBawl"
  110.                        menuLabel: "Consult 8Bawl..."
  111.                          menuKey: '\0'
  112.                         menuIcon: (NXImage *) face[0]];
  113.     return self;
  114. }
  115.  
  116. - init
  117. {
  118.     return self;
  119. }
  120.  
  121. - free
  122. {
  123.     [[NXApp inspector] invalidate];
  124.     return [super free];
  125. }
  126.  
  127. - initFromPboard:thePB inDoc:theDoc linked:(BOOL) linked {return [self init];}
  128.  
  129. // drawing loops -- trackMouse
  130. - (BOOL)    trackMouse:(NXEvent *)event
  131.             inRect:(const NXRect *)cellFrame
  132.             ofView:controlView    
  133. {
  134.     const NXPoint zero={ 0.0, 0.0 };
  135.     id c,b;
  136.     NXRect cr, crect,b8rect;
  137.     Window *cwin,*b8win;
  138.     NXPoint    point;
  139.     int k;
  140.     float dpt;
  141.     int b8gState, cgState;
  142.     static id bimage=nil, cimage=nil;
  143.  
  144.     [[NXApp inspector] inspect:[EBI new]];    
  145.     PSgsave();
  146.     PSobscurecursor();
  147.     PSsetwaitcursorenabled(NO);    // cheating!
  148.     //if (highlighted) PSsetgray(NX_LTGRAY);
  149.     //else 
  150.     PSsetgray([controlView backgroundGray]);
  151.     NXRectFill(cellFrame);
  152.     point = cellFrame->origin;
  153.     point.y += cellFrame->size.height;
  154.     [base8 composite:NX_SOVER toPoint:&point];
  155.     point.x += smallRectOrigin.x;
  156.     k = (int)(random()&0x7fffffff)%(NIMAGES-1) + 1;
  157.     if ((k-1)%3 == 0) point.y -= smallRectUpOrigin.y;
  158.     else point.y -= smallRectOrigin.y;
  159. //    dissolve base8 up to face[k]    
  160.  
  161.     dpt=0.0;
  162.     cr.size = smallRectSize;
  163.     cr.origin=((k-1)%3 == 0) ? smallRectUpOrigin : smallRectOrigin;
  164.  
  165.     [base8 getSize:&b8rect.size];
  166.     if (!bimage){ bimage=[[NXImage alloc] initSize:&b8rect.size];
  167.     [bimage useCacheWithDepth:NX_DefaultDepth];
  168.     }if ([bimage lockFocus]) {
  169.     [base8 composite:NX_SOVER toPoint:&bigRect.origin];
  170.     [bimage unlockFocus];
  171.     }
  172.     b=[bimage bestRepresentation];
  173.     [b getWindow:&b8win andRect:&b8rect];
  174.     b8gState=[b8win gState];
  175.     
  176.     if (!cimage) cimage=[[NXImage alloc] initSize:&smallRectSize];
  177.     [cimage setUnique:YES];    // this image must stay opaque!
  178.     [cimage setBackgroundColor:NX_COLORWHITE];
  179.     [cimage useCacheWithDepth:NX_DefaultDepth];
  180.     [cimage lockFocus];
  181.     [bimage composite:NX_SOVER fromRect:&cr toPoint:&zero];
  182.     [face[k] composite:NX_SOVER toPoint:&zero];
  183.     [cimage unlockFocus];
  184.     c=[cimage bestRepresentation];
  185.     [c getWindow:&cwin andRect:&crect];
  186.     cgState=[cwin gState];
  187.     
  188.     disup(dpt, 
  189.         cellFrame->origin.x+cr.origin.x, cellFrame->origin.y+288.0-cr.origin.y,
  190.         cr.size.width, cr.size.height, b8gState,
  191.         cellFrame->origin.x+cr.origin.x, cellFrame->origin.y+288.0-cr.origin.y,
  192.  
  193.         cellFrame->origin.x+cr.origin.x, cellFrame->origin.y+288.0-cr.origin.y,
  194.         cr.size.width, cr.size.height, cgState,    
  195.         cellFrame->origin.x+cr.origin.x, cellFrame->origin.y+288.0-cr.origin.y,
  196.         &dpt);
  197.  
  198. //    dissolve face[k] down to big8
  199.     point = cellFrame->origin;
  200.     point.y += cellFrame->size.height;
  201.     [big8 composite:NX_SOVER toPoint:&point];
  202.     PSgrestore();
  203.     PSsetwaitcursorenabled(YES);
  204.     return YES;
  205. }
  206. // default drawSelf
  207. - drawSelf:(const NXRect *)cellFrame inView:view        // MARGINALIA HERE!
  208. {
  209.     NXPoint    point;
  210.     
  211.     PSgsave();
  212.     if (highlighted) PSsetgray(NX_LTGRAY);
  213.     else PSsetgray([view backgroundGray]);
  214.     NXRectFill(cellFrame);
  215.     
  216.     point = cellFrame->origin;
  217.     point.y += cellFrame->size.height;
  218.     [big8 composite:NX_SOVER toPoint:&point];
  219.     PSgrestore();
  220.     return self;
  221. }
  222.  
  223. - calcCellSize:(NXSize *)theSize 
  224.     {theSize->width = theSize->height = 288.0; return self;}
  225. - highlight:(const NXRect *)rect inView:view lit:(BOOL)flag
  226.     {highlighted = !highlighted; NXHighlightRect(rect);return self;}
  227. - readRichText:(NXStream *)stream forView:view 
  228.     {return self;}
  229. - writeRichText:(NXStream *)stream forView:view
  230.     {return self;}
  231. @end